<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html>
<html>
<head>
	<title>药品价格解密</title>
	<%@ include file="/WEB-INF/component/commonCSS.jsp" %>
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    
    <section class="content">
        <table class="jqgrid" id="gridlist"></table>

    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp" %>
    <script type="text/javascript">
    	var flag = false;
    	var resultSuccess = false;
    	var resultMsg = "";
    	var dataLength = 0;
		var myData1 = [];
		var myData2 = [];
		var myData3 = [];
		$(function () {
			startDecrypt();
			$("#gridlist").jqGrid({
                datatype: "json",
                autowidth: true,
                height: 270,
                colNames: ['','','药品编码', '通用名',  '剂型', '规格', '转换系数', '单位', '材质','解密报价','解密时间'],
                colModel: [
					{ name: 'projProddrugId', width: 100,align: 'center',hidden:true },
					{ name: 'projId', width: 100,align: 'center',hidden:true },
					{ name: 'qualityspecCode', width: 100,align: 'center', sortable:false},
                    { name: 'qualityspecName', width: 140,align: 'left', sortable:false },
                    { name: 'formName', width: 80 ,align: 'center', sortable:false },
                    { name: 'specName', width: 80 ,align: 'left', sortable:false },
                    { name: 'factor', width: 80,align: 'center', sortable:false },
                    { name: 'packunitName', width: 40 ,align: 'center', sortable:false },
					{ name: 'materialName', width: 80 ,align: 'center', sortable:false },
                    { name: 'decryptPrice', width: 60, align: "right", sortable:false },
                    { name: 'decryptDatetime', width: 140, align: "center", sortable:false }
                ],
                rowNum: 0,
                rownumbers: true,
                caption: '药品报价解密',
                jsonReader: {
                    repeatitems: false,
                    id: "projProddrugId"
                },
                gridComplete: function() {
                	
                }
               
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
        });
		/* function close(){
			window.top.dialog({id:'projProddrugId'}).close();
			top.mainframe.search(); 
		} */
 	    function startDecrypt(){
 	    	$.post("${ctx}/drugPriceQuote/decryptPrice.html", {
				"projId" : "${projId}"
			}, function(result) {
				 dataLength = result.conditions.dataLength;
				 flag = true;
				if (result.success) {
					resultSuccess = true;
				} else {
						if(result.msg=="0"){
							resultMsg = "0";
						}
				}
			}, "json"); 
 	    }
 	    function gridLoad(){
 	    	$.post("${ctx}/drugPriceQuote/getDecryptData.html", {
 	    		"projId":"${projId}","datetime":"${datetime}"
			}, function(result) {
				if (result.success) {
					if(result.rows.length>0){
						addData(result);
					}else{
						delayLoadGrid();
					}
				}else{
					$.HN.message.alert("数据异常!", "消息", "error");
					$("#gridlist")[0].addJSONData(myData2);
				}
			}, "json"); 
 	    	
 	    }
 	   setTimeout(delayLoadGrid,1000); 
 	   
 	   function delayLoadGrid(){
 		  if(flag){
			if (!resultSuccess) {
				if(resultMsg=="0"){
					$.HN.message.alert("没有解密数据!", "消息", "error");
					return;
				}else{
					$.HN.message.alert("数据异常", "消息", "error");
					return;
				}
			} 
		 }
 		 gridLoad();
 	   }
 	 
 	   var i=0,t;
 	   function addData(obj){
 		  //myData1 = [{projProddrugId:obj.rows[i].projProddrugId,projId:obj.rows[i].projId,qualityspecCode:obj.rows[i].qualityspecCode,qualityspecName:obj.rows[i].qualityspecName,formName:obj.rows[i].formName,specName:obj.rows[i].specName,factor:obj.rows[i].factor,packunitName:obj.rows[i].packunitName,materialName:obj.rows[i].materialName,decryptPrice:obj.rows[i].decryptPrice,decryptDatetime:obj.rows[i].decryptDatetime}];
 		  myData1 =  [].concat(obj.rows[i]);
 		 myData2  = myData1.concat(myData2);
 		  if(myData2.length>10){
 			  for(var j=0;j<10;j++){
 				  myData3 = ([].concat(myData2[9-j])).concat(myData3);
 			  }  
 			 $("#gridlist")[0].addJSONData(myData3);
 			myData3 = [];
 			for(var k=0;k<10;k++){
 				$($("td[aria-describedby='gridlist_rn']")[9-k]).html((i+1-k)+"");
 	 		  }
 		   }else{
 			  $("#gridlist")[0].addJSONData(myData2);
 		   }
 		 
 		  //计算 rownum元素的宽度
 		 var len = (((i+1)+"").length)*10;
 		$('.jqgfirstrow').find('td').eq(0).css('width',len+'px');
     	$('#gridlist_rn').css('width',len+'px');

 		 $("#gridlist").setCaption("药品报价解密           共解密:"+(i+1)+"条");  
 			  i++;
 			 t = setTimeout(function(){addData(obj)}, 50);
 			 if ( i >= obj.rows.length ) {
 				 i = obj.rows.length;
 				   clearTimeout(t);
 				   if(dataLength==0){
 					  gridLoad();
 				   }else if(obj.rows.length<dataLength){
 					  gridLoad();
 				   }else if(flag){
 					  if (resultSuccess) {
 							$.HN.message.alert("解密成功"+dataLength+"条!", "消息", "success");
 							$("#gridlist")[0].addJSONData(myData2);
 						} else {
 								if(resultMsg=="0"){
 									//$.HN.message.alert("没有解密数据!", "消息", "error");
 								}else{
 									$.HN.message.alert("数据异常", "消息", "error");
 									$("#gridlist")[0].addJSONData(myData2);
 								}
 						}
 				   }
 				 }
 	   }
 	   
    </script>
</body>
</html>